package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.task;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.util.StrUtil;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.TaskCreatedPostProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowNodeParticipantSettingVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowProcessInstanceTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowNodeParticipantSettingRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowProcessInstanceTraceRepository;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2021/1/6 下午4:48
 */
@Service
@Order(18)
public class SimpleSetParticipantPostProcessor implements TaskCreatedPostProcessor {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FlowProcessInstanceTraceRepository processInstanceTraceRepository;

    @Autowired
    private FlowNodeParticipantSettingRepository nodeParticipantSettingRepository;

    @Override
    public void process(TaskEntity taskEntity) {
        String modelKey = resolveModelKey(taskEntity);
        if (Objects.isNull(modelKey)) {
            logger.warn("获取流程编码失败，流程实例id为：{}", taskEntity.getProcessInstanceId());
            return;
        }

        FlowNodeParticipantSettingVo nodeSetting = nodeParticipantSettingRepository.getNodeSetting(modelKey, taskEntity.getTaskDefinitionKey());
        if (Objects.nonNull(nodeSetting) && StrUtil.isNotBlank(nodeSetting.getParticipantId())) {
            logger.info("任务节点：{},设置处理人为:{}-{}", taskEntity.getName(), nodeSetting.getParticipantId(), nodeSetting.getParticipantName());
            taskEntity.setAssignee(nodeSetting.getParticipantId());
        }
    }

    private String resolveModelKey(TaskEntity taskEntity) {
        String modelKey = resolveByProcessDefinitionId(taskEntity.getProcessDefinitionId());
        if (Objects.nonNull(modelKey)) {
            return modelKey;
        }
        FlowProcessInstanceTraceVo piTrace = processInstanceTraceRepository.getProcessInstanceTraceByProcessInstanceId(taskEntity.getProcessInstanceId());
        if (Objects.nonNull(piTrace)) {
            return piTrace.getModelKey();
        }
        return null;
    }

    private String resolveByProcessDefinitionId(String processDefinitionId) {
        if (StrUtil.isNotBlank(processDefinitionId) && processDefinitionId.contains(":")) {
            List<String> split = StrUtil.split(processDefinitionId, ':', 3);
            if (Objects.equals(split.size(), 3) && StrUtil.isNotBlank(split.get(0))) {
                return split.get(0);
            }
        }

        return null;
    }
}
